<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/8/28
 * Time: 15:09
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\MemberModel;
class Member extends Base
{
    /*
     * 用户管理首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['user_username|user_phone'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['user_time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od=$field." ".$order;
            }else{
                $od="user_time desc";
            }
            $m = new MemberModel();
            $count = $m->getAllcount($map);
            $lists = $m->getMemeberData($map,$od, $Nowpage, $limits);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('member/index');
    }

    /*
     * 用户收货地址
     */
    public function address(){
        $id = input('id');
        $m = new MemberModel();
        $data = $m->getAddress($id);
        return $this->fetch('member/address',['data'=>$data]);
    }

    /*
     * 删除用户
     */
    public function memberDel(){
        $id = input('id');
        $m = new MemberModel();
        $flag = $m->memberDel($id);
        return json(['code'=>$flag['code'],'msg'=>$flag['msg']]);
    }

    public function user_state()
    {
        extract(input());
        $role = new MemberModel();
        $flag = $role->memberState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
}
